package afcp.alumni.model.db;

import java.io.Serializable;

import javax.jdo.annotations.NullValue;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import afcp.alumni.model.base.UniqueField;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.datanucleus.annotations.Unowned;

@PersistenceCapable
public class Login implements UniqueField<Member>, Serializable {
	
	private static final long serialVersionUID = 1358634122491647682L;

	public Login() {
	}
	
	public Login(String id) {
		setId(id);
	}
	
	public Login(String id, Member member) {
		setId(id);
		this.member = member;
	}
	
	@PrimaryKey
	@Persistent
	// (valueStrategy = IdGeneratorStrategy.IDENTITY) // To allow automatic
	// generation when saving to datastore
	private Key key;
	@Persistent
	private String id;
	@Persistent(nullValue=NullValue.EXCEPTION)
	@Unowned
//	@Column(allowsNull="false")
	private Member member;

	@Override
	public String getId() {
		return id;
	}

	@Override
	public Key getKey() {
		return key;
	}

//	@Override
//	public void setKey() {
//		this.key = KeyFactory.createKey(null, this.getClass().getSimpleName(), id);
//	}

	@Override
	public void setId(String id) {
		this.id = id;
		this.key = KeyFactory.createKey(null, this.getClass().getSimpleName(), id);
	}

	@Override
	public Member getEntity() {
		return member;
	}

	@Override
	public void setEntity(Member entity) {
		member = entity;
	}

	@Override
	public Class<Member> entityClass() {
		return Member.class;
	}
	
	@Override
	public String toString() {
		return id;
	}

}
